package com.elinkway.infinitemovies.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HeaderViewListAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.elinkway.infinitemovies.adapter.SearchResultAdapter;
import com.elinkway.infinitemovies.bean.SearchResultType;
import com.elinkway.infinitemovies.utils.UIs;
import com.le123.ysdq.R;

import java.util.List;

public class SearchResultTabStrip extends HorizontalScrollView {
	private LinearLayout tabsContainer;
	private int currentPosition = 0;
	private Context mContext;
	private OnTabClickListener mOnTabClickListener;
	private SearchResultAdapter mAdapter;
	private ListView mListView;
	
	/**
	 * resultType
	 * 分类列表数据集
	 */
	private List<SearchResultType> resultType ;
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);

	}
	private final int SELECTED_COLOR = 0xff000000;
	private final int UNSELECTED_COLOR = 0xffa2a2aa;
	public SearchResultTabStrip(Context context) {
		this(context,null,0);
	}

	public SearchResultTabStrip(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		setFillViewport(true);
		this.mContext = context;
		tabsContainer = new LinearLayout(context);
		tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
		tabsContainer.setLayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		addView(tabsContainer);
	}

	public SearchResultTabStrip(Context context, AttributeSet attrs) {
		this(context, attrs,0);
		// TODO Auto-generated constructor stub
	}
	public void setOnTabClickListener(OnTabClickListener listener){
		this.mOnTabClickListener = listener;
	}
	public void setList(ListView listView){
		if (listView.getAdapter() == null) {
			throw new IllegalStateException(
					"ListView does not have adapter instance.");
		}
		this.mListView=listView;
		if(listView.getAdapter() instanceof HeaderViewListAdapter){
			HeaderViewListAdapter adapter = (HeaderViewListAdapter) listView.getAdapter();
			this.mAdapter=(SearchResultAdapter) adapter.getWrappedAdapter();
		}
		else{
			this.mAdapter=(SearchResultAdapter) listView.getAdapter();
		}
		notifyDataSetChanged();
	}
	public void notifyDataSetChanged() {
		if(mAdapter!=null){
			tabsContainer.removeAllViews();
			resultType = mAdapter.getSearchResultList().getResultType();
			SearchResultType firstTab = new SearchResultType();
			firstTab.setChannelname("全部");
			firstTab.setRecords(Integer.toString(mAdapter.getSearchResultList().getTotalRecords()));
			firstTab.setVt("");
			addTextTab(0,firstTab);
			for(int i=0;i<resultType.size();i++){
				addTextTab(i+1,resultType.get(i));
			}
			
			ImageView filler = new ImageView(mContext);
			filler.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			filler.setBackgroundResource(R.drawable.search_tab_unselect);
			filler.setFocusable(false);
			filler.setClickable(false);
			filler.setEnabled(false);
			tabsContainer.addView(filler);
		}
	}
	private void addTextTab(final int position, SearchResultType tabData) {

		TextView tab = (TextView) UIs.inflate(mContext, R.layout.search_tab_text, null);
		tab.setText(String.format("%1$s",tabData.getChannelname()));
		tab.setTag(tabData);
		tab.setFocusable(true);
		if(position==0){
			tab.setTextColor(SELECTED_COLOR);
			tab.setSelected(true);
		}
		else{
			tab.setTextColor(UNSELECTED_COLOR);
		}
		tab.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(currentPosition!=position) {
					selectTab(position);
					if(mOnTabClickListener!=null){
						mOnTabClickListener.onTabClick(position, v);
					}
				}
			}
		});
		tabsContainer.addView(tab, position);
	}
	public void setCurrentPosition(int position){
		this.currentPosition=position;
	}
	public interface OnTabClickListener{
		void  onTabClick(int position ,View v);
	}
	public synchronized void selectTab(int position){
		if(currentPosition!=position){
			for(int i = 0 ;i<=resultType.size();i++){
				TextView tab = (TextView) tabsContainer.getChildAt(i);
				if(i!=position && tab.isSelected()) {
					tab.setSelected(false);
					tab.setTextColor(UNSELECTED_COLOR);
				}
				else if(i==position && !tab.isSelected()) {
					tab.setSelected(true);
					tab.setTextColor(SELECTED_COLOR);
				}
			}
			currentPosition=position;
			TextView tab = (TextView) tabsContainer.getChildAt(position);
			final Rect tabRect = new Rect(0, 0, tab.getWidth(), tab.getHeight());
			final Rect tabVisibleRect = new Rect(tabRect);
			tabsContainer.getChildVisibleRect(tab, tabVisibleRect, null);
			if(tabRect.width()>tabVisibleRect.width()){
				if(tabVisibleRect.left==0){
					postDelayed(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							smoothScrollBy(-(tabRect.width()-tabVisibleRect.width()),0);
						}
					},50l);
				}
				else {
					postDelayed(new Runnable() {
						
						@Override
						public void run() {
							smoothScrollBy((tabRect.width()-tabVisibleRect.width()),0);
						}
					},50l);
				}
			}
		}
	}

}
